package com.itheima.sort;

public class InsertSort {
    public static void main(String[] args) {

        int[] arr={3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};

        //1.先找到无序组是从哪个下标开始的
        int index = -1;
        for (int i = 0; i < arr.length-1; i++) {
            if(arr[i]> arr[i+1]){
                index = i+1;
                break;
            }
        }
        if(index==-1){
            System.out.println("原数组已经有序");
        }

        for (int i = index; i < arr.length; i++) {
            int j = i;
            while (j>0) {
                if (arr[j]<arr[j-1]){
                    int temp = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = temp;
                    j--;
                }else {
                    break;
                }

            }
        }
        Print(arr);
    }
    private static void Print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
